package geektime.spring.springbucks.api;

import geektime.spring.springbucks.model.Coffee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Created by PC on 2022/7/7.
 */
@Service
@Slf4j
public class IpInfoService {
    /**
     * 第一个zkServer是配置文件种的spring.application.name,第二个zkApi是controller中配置的路径
     */
    public static String remoteIpServiceUrl = "http://coffee-service/coffee/findAll";
    @Autowired
    private RestTemplate restTemplate;

    public void getIpInfo() {
        ParameterizedTypeReference<List<Coffee>> ptr =
                new ParameterizedTypeReference<List<Coffee>>() {};
        ResponseEntity<List<Coffee>> list = restTemplate
                .exchange(remoteIpServiceUrl, HttpMethod.GET, null, ptr);
        list.getBody().forEach(c -> log.info("Coffee: {}", c));
    }
}
